home *** CD-ROM | disk | FTP | other *** search
/ Guide To Cracking 2002 / Guide_to_Cracking_2002.iso / Utilities / Password Crackerz / Xit.exe / CRYPT.C next >
C/C++ Source or Header  |  1994-01-01  |  22KB  |  723 lines

  1. // "@(#) Crypt[XiT], Variation/Optimization of crypt.c 5.11 xx/10/93";
  2.  
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include "pwd.h"
  6.  
  7. /* =====  Configuration ==================== */
  8.  
  9. /*
  10.  * define "LARGEDATA" to get faster permutations, by using about 72 kilobytes
  11.  * of lookup tables.  This speeds up des_setkey() and des_cipher(), but has
  12.  * little effect on crypt().
  13.  */
  14. //#define   LARGEDATA
  15.  
  16. /* compile with "-DSTATIC=int" when profiling */
  17. #ifndef STATIC
  18. #define STATIC   static
  19. #endif
  20. STATIC init_des(), init_perm(), permute();
  21.  
  22. #ifdef DEBUG
  23. STATIC prtab();
  24. #endif
  25.  
  26. typedef union {
  27.    unsigned char far b[8];
  28.    struct {
  29.       /* long is often faster than a 32-bit bit field */
  30.       long   far i0;
  31.       long   far i1;
  32.    } b32;
  33. } C_block;
  34.  
  35. /*
  36.  * Convert twenty-four-bit long in host-order
  37.  * to six bits (and 2 low-order zeroes) per char little-endian format.
  38. */
  39. // Shifting src by 6,12 and 18 is faster than increasing src by 6 each step.
  40. #define   TO_SIX_BIT(rslt, src) {                     \
  41.       C_block cvt;                                 \
  42.       cvt.b[0] = src;          cvt.b[1] = src>>6;    \
  43.       cvt.b[2] = src>>12;      cvt.b[3] = src>>18;   \
  44.       rslt = (cvt.b32.i0 & 0x3f3f3f3fL) << 2;      \
  45. }
  46.  
  47. /*
  48.  * These macros may someday permit efficient use of 64-bit integers.
  49.  */
  50. #define   ZERO(d,d0,d1)         d0 = 0, d1 = 0
  51. #define   LOAD(d,d0,d1,bl)      d0 = (bl).b32.i0, d1 = (bl).b32.i1
  52. #define   LOADREG(d,d0,d1,s,s0,s1)   d0 = s0, d1 = s1
  53. #define   OR(d,d0,d1,bl)         d0 |= (bl).b32.i0, d1 |= (bl).b32.i1
  54. #define   STORE(s,s0,s1,bl)      (bl).b32.i0 = s0, (bl).b32.i1 = s1
  55. #define   DCL_BLOCK(d,d0,d1)   long d0, d1
  56.  
  57. #if defined(LARGEDATA)
  58.    /* Waste memory like crazy.  Also, do permutations in line */
  59. #define   LGCHUNKBITS   3
  60. #define   CHUNKBITS   (1<<LGCHUNKBITS)
  61. #define   PERM6464(d,d0,d1,cpp,p)            \
  62.    LOAD(d,d0,d1,(p)[(0<<CHUNKBITS)+(cpp)[0]]);      \
  63.    OR (d,d0,d1,(p)[(1<<CHUNKBITS)+(cpp)[1]]);      \
  64.    OR (d,d0,d1,(p)[(2<<CHUNKBITS)+(cpp)[2]]);      \
  65.    OR (d,d0,d1,(p)[(3<<CHUNKBITS)+(cpp)[3]]);      \
  66.    OR (d,d0,d1,(p)[(4<<CHUNKBITS)+(cpp)[4]]);      \
  67.    OR (d,d0,d1,(p)[(5<<CHUNKBITS)+(cpp)[5]]);      \
  68.    OR (d,d0,d1,(p)[(6<<CHUNKBITS)+(cpp)[6]]);      \
  69.    OR (d,d0,d1,(p)[(7<<CHUNKBITS)+(cpp)[7]]);
  70. #define   PERM3264(d,d0,d1,cpp,p)            \
  71.    LOAD(d,d0,d1,(p)[(0<<CHUNKBITS)+(cpp)[0]]);      \
  72.    OR (d,d0,d1,(p)[(1<<CHUNKBITS)+(cpp)[1]]);      \
  73.    OR (d,d0,d1,(p)[(2<<CHUNKBITS)+(cpp)[2]]);      \
  74.    OR (d,d0,d1,(p)[(3<<CHUNKBITS)+(cpp)[3]]);
  75. #else
  76.    /* "small data" */
  77. #define   LGCHUNKBITS   2
  78. #define   CHUNKBITS   4 //(1<<LGCHUNKBITS)
  79. #define   PERM6464(d,d0,d1,cpp,p)            \
  80.    { C_block tblk; permute8(cpp,&tblk,p); LOAD (d,d0,d1,tblk); }
  81. #define   PERM3264(d,d0,d1,cpp,p)            \
  82.    { C_block tblk; permute4(cpp,&tblk,p); LOAD (d,d0,d1,tblk); }
  83.  
  84. STATIC permute8(cp, out, p)
  85.    unsigned char *cp;
  86.    C_block *out;
  87.    register C_block *p;
  88. {
  89.    register DCL_BLOCK(D,D0,D1);
  90.    register C_block *tp;
  91.    register int t, chars_in=8;
  92.  
  93.    ZERO(D,D0,D1);
  94.    do {
  95.       t = *cp++;
  96.       tp = &p[t&0xf]; OR(D,D0,D1,*tp); p += 16; // (1<<CHUNKBITS);
  97.       tp = &p[t>>4];  OR(D,D0,D1,*tp); p += 16; // (1<<CHUNKBITS);
  98.    } while (--chars_in > 0);
  99.    STORE(D,D0,D1,*out);
  100. }
  101.  
  102. STATIC permute4(cp, out, p)
  103.    unsigned char *cp;
  104.    C_block *out;
  105.    register C_block *p;
  106. {
  107.    register DCL_BLOCK(D,D0,D1);
  108.    register C_block *tp;
  109.    register int t, chars_in=4;;
  110.  
  111.    ZERO(D,D0,D1);
  112.    do {
  113.       t = *cp++;
  114.       tp = &p[t&0xf]; OR(D,D0,D1,*tp); p += (1<<CHUNKBITS);
  115.       tp = &p[t>>4];  OR(D,D0,D1,*tp); p += (1<<CHUNKBITS);
  116.    } while (--chars_in > 0);
  117.    STORE(D,D0,D1,*out);
  118. }
  119. #endif /* LARGEDATA */
  120.  
  121.  
  122. /* =====  (mostly) Standard DES Tables ==================== */
  123.  
  124. static unsigned char IP[] = {      /* initial permutation */
  125.    58, 50, 42, 34, 26, 18, 10,  2,
  126.    60, 52, 44, 36, 28, 20, 12,  4,
  127.    62, 54, 46, 38, 30, 22, 14,  6,
  128.    64, 56, 48, 40, 32, 24, 16,  8,
  129.    57, 49, 41, 33, 25, 17,  9,  1,
  130.    59, 51, 43, 35, 27, 19, 11,  3,
  131.    61, 53, 45, 37, 29, 21, 13,  5,
  132.    63, 55, 47, 39, 31, 23, 15,  7,
  133. };
  134.  
  135. /* The final permutation is the inverse of IP - no table is necessary */
  136.  
  137. static unsigned char ExpandTr[] = {   /* expansion operation */
  138.    32,  1,  2,  3,  4,  5,
  139.     4,  5,  6,  7,  8,  9,
  140.     8,  9, 10, 11, 12, 13,
  141.    12, 13, 14, 15, 16, 17,
  142.    16, 17, 18, 19, 20, 21,
  143.    20, 21, 22, 23, 24, 25,
  144.    24, 25, 26, 27, 28, 29,
  145.    28, 29, 30, 31, 32,  1,
  146. };
  147.  
  148. static unsigned char PC1[] = {      /* permuted choice table 1 */
  149.    57, 49, 41, 33, 25, 17,  9,
  150.     1, 58, 50, 42, 34, 26, 18,
  151.    10,  2, 59, 51, 43, 35, 27,
  152.    19, 11,  3, 60, 52, 44, 36,
  153.  
  154.    63, 55, 47, 39, 31, 23, 15,
  155.     7, 62, 54, 46, 38, 30, 22,
  156.    14,  6, 61, 53, 45, 37, 29,
  157.    21, 13,  5, 28, 20, 12,  4,
  158. };
  159.  
  160. static unsigned char Rotates[] = {   /* PC1 rotation schedule */
  161.    1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
  162. };
  163.  
  164. /* note: each "row" of PC2 is left-padded with bits that make it invertible */
  165. static unsigned char PC2[] = {      /* permuted choice table 2 */
  166.     9, 18,    14, 17, 11, 24,  1,  5,
  167.    22, 25,     3, 28, 15,  6, 21, 10,
  168.    35, 38,    23, 19, 12,  4, 26,  8,
  169.    43, 54,    16,  7, 27, 20, 13,  2,
  170.  
  171.     0,  0,    41, 52, 31, 37, 47, 55,
  172.     0,  0,    30, 40, 51, 45, 33, 48,
  173.     0,  0,    44, 49, 39, 56, 34, 53,
  174.     0,  0,    46, 42, 50, 36, 29, 32,
  175. };
  176.  
  177. static unsigned char S[8][64] = {   /* 48->32 bit substitution tables */
  178.                /* S[1]         */
  179.    14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  180.     0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  181.     4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  182.    15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  183.                /* S[2]         */
  184.    15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  185.     3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  186.     0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  187.    13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  188.                /* S[3]         */
  189.    10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  190.    13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  191.    13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  192.     1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  193.                /* S[4]         */
  194.     7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  195.    13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  196.    10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  197.     3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  198.                /* S[5]         */
  199.     2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  200.    14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  201.     4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  202.    11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  203.                /* S[6]         */
  204.    12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  205.    10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  206.     9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  207.     4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  208.                /* S[7]         */
  209.     4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  210.    13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  211.     1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  212.     6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  213.                /* S[8]         */
  214.    13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  215.     1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  216.     7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  217.     2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11,
  218. };
  219.  
  220. static unsigned char P32Tr[] = {   /* 32-bit permutation function */
  221.    16,  7, 20, 21,
  222.    29, 12, 28, 17,
  223.     1, 15, 23, 26,
  224.     5, 18, 31, 10,
  225.     2,  8, 24, 14,
  226.    32, 27,  3,  9,
  227.    19, 13, 30,  6,
  228.    22, 11,  4, 25,
  229. };
  230.  
  231. static unsigned char CIFP[] = {      /* compressed/interleaved permutation */
  232.     1,  2,  3,  4,   17, 18, 19, 20,
  233.     5,  6,  7,  8,   21, 22, 23, 24,
  234.     9, 10, 11, 12,   25, 26, 27, 28,
  235.    13, 14, 15, 16,   29, 30, 31, 32,
  236.  
  237.    33, 34, 35, 36,   49, 50, 51, 52,
  238.    37, 38, 39, 40,   53, 54, 55, 56,
  239.    41, 42, 43, 44,   57, 58, 59, 60,
  240.    45, 46, 47, 48,   61, 62, 63, 64,
  241. };
  242.  
  243. static unsigned char far itoa64[] =      /* 0..63 => ascii-64 */
  244.    "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  245.  
  246.  
  247. /* =====  Tables that are initialized at run time  ==================== */
  248.  
  249.  
  250. static unsigned char a64toi[128];   /* ascii-64 => 0..63 */
  251.  
  252. /* Initial key schedule permutation */
  253. static C_block   PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
  254.  
  255. /* Subsequent key schedule rotation permutations */
  256. static C_block   PC2ROT[2][64/CHUNKBITS][1<<CHUNKBITS];
  257.  
  258. /* Initial permutation/expansion table */
  259. static C_block   IE3264[32/CHUNKBITS][1<<CHUNKBITS];
  260.  
  261. /* Table that combines the S, P, and E operations.  */
  262. static long SPE[2][8][64];
  263.  
  264. /* compressed/interleaved => final permutation table */
  265. static C_block   CF6464[64/CHUNKBITS][1<<CHUNKBITS];
  266.  
  267.  
  268. /* ==================================== */
  269.  
  270.  
  271. static C_block   constdatablock;         /* encryption constant */
  272. static char   cryptresult[1+4+4+11+1];   /* encrypted result */
  273.  
  274. /*
  275.  * Return a pointer to static data consisting of the "setting"
  276.  * followed by an encryption produced by the "key" and "setting".
  277.  */
  278. char *crypt(key, setting)
  279.    register const char *key;
  280.    register const char *setting; {
  281.    register char *encp;
  282.    register long i;
  283.    register int t;
  284.    long salt;
  285.    C_block keyblock, rsltblock;
  286.  
  287.    for (i = 0; i < 8; i++) {
  288.       if ((t = 2*(unsigned char)(*key)) != 0)
  289.          key++;
  290.       keyblock.b[i] = t;
  291.    }
  292.    if (des_setkey((char *)keyblock.b))   /* also initializes "a64toi" */
  293.       return (NULL);
  294.  
  295.    encp = &cryptresult[0];
  296.    //---------------------
  297.    salt = 0;
  298.    for (i = 2; --i >= 0; ) {
  299.       if ((t = (unsigned char)setting[i]) == '\0')
  300.          t = '.';
  301.       encp[i] = t;
  302.       salt = (salt<<6) | a64toi[t];
  303.    }
  304.    encp += 2;
  305.    if (des_cipher((char *)&constdatablock, (char *)&rsltblock, salt))
  306.       return (NULL);
  307.  
  308.    /*
  309.     * Encode the 64 cipher bits as 11 ascii characters.
  310.     */
  311.    i = ((long)((rsltblock.b[0]<<8) | rsltblock.b[1])<<8) | rsltblock.b[2];
  312.    encp[3] = itoa64[i&0x3f];   i >>= 6;
  313.    encp[2] = itoa64[i&0x3f];   i >>= 6;
  314.    encp[1] = itoa64[i&0x3f];   i >>= 6;
  315.    encp[0] = itoa64[i&0x3f];   encp += 4;
  316.    i = ((long)((rsltblock.b[3]<<8) | rsltblock.b[4])<<8) | rsltblock.b[5];
  317.    encp[3] = itoa64[i&0x3f];   i >>= 6;
  318.    encp[2] = itoa64[i&0x3f];   i >>= 6;
  319.    encp[1] = itoa64[i&0x3f];   i >>= 6;
  320.    encp[0] = itoa64[i&0x3f];   encp += 4;
  321.    i = ((long)((rsltblock.b[6])<<8) | rsltblock.b[7])<<2;
  322.    encp[2] = itoa64[i&0x3f];   i >>= 6;
  323.    encp[1] = itoa64[i&0x3f];   i >>= 6;
  324.    encp[0] = itoa64[i&0x3f];
  325.  
  326.    encp[3] = 0;
  327.  
  328.    return (cryptresult);
  329. }
  330.  
  331.  
  332. /*
  333.  * The Key Schedule, filled in by des_setkey() or setkey().
  334.  */
  335. #define   KS_SIZE   16
  336. static C_block   KS[KS_SIZE];
  337.  
  338. /*
  339.  * Set up the key schedule from the key.
  340.  */
  341. des_setkey(key)
  342.    register const char *key; {
  343.    register DCL_BLOCK(K, K0, K1);
  344.    register C_block *ptabp;
  345.    register int i;
  346.    static int des_ready = 0;
  347.  
  348.    if (!des_ready) {
  349.       init_des();
  350.       des_ready = 1;
  351.    }
  352.  
  353.    PERM6464(K,K0,K1,(unsigned char *)key,(C_block *)PC1ROT);
  354.    key = (char *)&KS[0];
  355.    STORE(K&~0x03030303L, K0&~0x03030303L, K1, *(C_block *)key);
  356.    for (i = 1; i < 16; i++) {
  357.       key += sizeof(C_block);
  358.       STORE(K,K0,K1,*(C_block *)key);
  359.       ptabp = (C_block *)PC2ROT[Rotates[i]-1];
  360.       PERM6464(K,K0,K1,(unsigned char *)key,ptabp);
  361.       STORE(K&~0x03030303L, K0&~0x03030303L, K1, *(C_block *)key);
  362.    }
  363.    return (0);
  364. }
  365.  
  366. /*
  367.  * Encrypt (or decrypt if num_iter < 0) the 8 chars at "in" with abs(num_iter)
  368.  * iterations of DES, using the the given 24-bit salt and the pre-computed key
  369.  * schedule, and store the resulting 8 chars at "out" (in == out is permitted).
  370.  *
  371.  * NOTE: the performance of this routine is critically dependent on your
  372.  * compiler and machine architecture.
  373.  */
  374. des_cipher(in, out, salt)
  375.    const char *in;
  376.    char *out;
  377.    long salt; {
  378.    /* variables that we want in registers, most important first */
  379. #if defined(pdp11)
  380.    register int j;
  381. #endif
  382.    register long L0, L1, R0, R1, k;
  383.    register C_block *kp;
  384.    register int loop_count, num_iter;
  385.    C_block B;
  386.  
  387.    L0 = salt;
  388.    TO_SIX_BIT(salt, L0);   /* convert to 4*(6+2) format */
  389.  
  390. //#if defined(vax) || defined(pdp11)
  391. //   salt = ~salt;   /* "x &~ y" is faster than "x & y". */
  392. //#define   SALT (~salt)
  393. //#else
  394. #define   SALT salt
  395. //#endif
  396.  
  397. //#if defined(MUST_ALIGN)
  398. //   B.b[0] = in[0]; B.b[1] = in[1]; B.b[2] = in[2]; B.b[3] = in[3];
  399. //   B.b[4] = in[4]; B.b[5] = in[5]; B.b[6] = in[6]; B.b[7] = in[7];
  400. //   LOAD(L,L0,L1,B);
  401. //#else
  402.    LOAD(L,L0,L1,*(C_block *)in);
  403. //#endif
  404.  
  405.    LOADREG(R,R0,R1,L,L0,L1);
  406. //   if(L0 || L1)
  407. //      puts("Sorry!!\n");
  408. //   L0 &= 0x55555555L;
  409. //   L1 &= 0x55555555L;
  410. //   L0 = (L0 << 1) | L1;   /* L0 is the even-numbered input bits */
  411. //   R0 &= 0xaaaaaaaaL;
  412. //   R1 = (R1 >> 1) & 0x55555555L;
  413. //   L1 = R0 | R1;      /* L1 is the odd-numbered input bits */
  414.    STORE(L,L0,L1,B);
  415.    PERM3264(L,L0,L1,B.b,  (C_block *)IE3264);   /* even bits */
  416.    PERM3264(R,R0,R1,B.b+4,(C_block *)IE3264);   /* odd bits */
  417.  
  418. //   if (num_iter >= 0)
  419. //   {      /* encryption */
  420.       kp = &KS[0];
  421. //      ks_inc  = sizeof(*kp);
  422. //   }
  423. //   else
  424. //   {      /* decryption */
  425. //      num_iter = -num_iter;
  426. //      kp = &KS[KS_SIZE-1];
  427. //      ks_inc  = -sizeof(*kp);
  428. //   }
  429.    num_iter=25;
  430.    while (--num_iter >= 0) {
  431.       loop_count = 8;
  432.       do {
  433.  
  434. #define   SPTAB(t, i)   (*(long *)((unsigned char *)t + i*(sizeof(long)/4)))
  435.  
  436. #if defined(gould)
  437.          /* use this if B.b[i] is evaluated just once ... */
  438. #define   DOXOR(x,y,i)   x^=SPTAB(SPE[0][i],B.b[i]); y^=SPTAB(SPE[1][i],B.b[i]);
  439. #else
  440. #if defined(pdp11)
  441.          /* use this if your "long" int indexing is slow */
  442. #define   DOXOR(x,y,i)   j=B.b[i]; x^=SPTAB(SPE[0][i],j); y^=SPTAB(SPE[1][i],j);
  443. #else
  444.          /* use this if "k" is allocated to a register ... */
  445. #define   DOXOR(x,y,i)   k=B.b[i]; x^=SPTAB(SPE[0][i],k); y^=SPTAB(SPE[1][i],k);
  446. #endif
  447. #endif
  448.  
  449. // 5 lines below it was      kp = (C_block *)((char *)kp+ks_inc);   \
  450. #define   CRUNCH(p0, p1, q0, q1)   \
  451.          k = (q0 ^ q1) & SALT;   \
  452.          B.b32.i0 = k ^ q0 ^ kp->b32.i0;      \
  453.          B.b32.i1 = k ^ q1 ^ kp->b32.i1;      \
  454.          kp = (C_block *)((char *)kp+8);     \
  455.                      \
  456.          DOXOR(p0, p1, 0);      \
  457.          DOXOR(p0, p1, 1);      \
  458.          DOXOR(p0, p1, 2);      \
  459.          DOXOR(p0, p1, 3);      \
  460.          DOXOR(p0, p1, 4);      \
  461.          DOXOR(p0, p1, 5);      \
  462.          DOXOR(p0, p1, 6);      \
  463.          DOXOR(p0, p1, 7);
  464.  
  465.          CRUNCH(L0, L1, R0, R1);
  466.          CRUNCH(R0, R1, L0, L1);
  467.       } while (--loop_count != 0);
  468.       kp = (C_block *)((char *)kp-(128));   // ks_inc*KS_SIZE= 8 * 16 = 128
  469.  
  470.  
  471.       /* swap L and R */
  472.       L0 ^= R0;  L1 ^= R1;
  473.       R0 ^= L0;  R1 ^= L1;
  474.       L0 ^= R0;  L1 ^= R1;
  475.    }
  476.  
  477.    /* store the encrypted (or decrypted) result */
  478.    L0 = ((L0 >> 3) & 0x0f0f0f0fL) | ((L1 << 1) & 0xf0f0f0f0L);
  479.    L1 = ((R0 >> 3) & 0x0f0f0f0fL) | ((R1 << 1) & 0xf0f0f0f0L);
  480.    STORE(L,L0,L1,B);
  481.    PERM6464(L,L0,L1,B.b, (C_block *)CF6464);
  482. #if defined(MUST_ALIGN)
  483.    STORE(L,L0,L1,B);
  484.    out[0] = B.b[0]; out[1] = B.b[1]; out[2] = B.b[2]; out[3] = B.b[3];
  485.    out[4] = B.b[4]; out[5] = B.b[5]; out[6] = B.b[6]; out[7] = B.b[7];
  486. #else
  487.    STORE(L,L0,L1,*(C_block *)out);
  488. #endif
  489.    return (0);
  490. }
  491.  
  492.  
  493. /*
  494.  * Initialize various tables.  This need only be done once.  It could even be
  495.  * done at compile time, if the compiler were capable of that sort of thing.
  496.  */
  497. STATIC init_des() {
  498.    register int i, j;
  499.    register long k;
  500.    register int tableno;
  501.    static unsigned char perm[64], tmp32[32];   /* "static" for speed */
  502.  
  503.    /*
  504.     * table that converts chars "./0-9A-Za-z"to integers 0-63.
  505.     */
  506.    for (i = 0; i < 64; i++)
  507.       a64toi[itoa64[i]] = i;
  508.  
  509.    /*
  510.     * PC1ROT - bit reverse, then PC1, then Rotate, then PC2.
  511.     */
  512.    for (i = 0; i < 64; i++)
  513.       perm[i] = 0;
  514.    for (i = 0; i < 64; i++) {
  515.       if ((k = PC2[i]) == 0)
  516.          continue;
  517.       k += Rotates[0]-1;
  518.       if ((k%28) < Rotates[0]) k -= 28;
  519.       k = PC1[k];
  520.       if (k > 0) {
  521.          k--;
  522.          k = (k|07) - (k&07);
  523.          k++;
  524.       }
  525.       perm[i] = k;
  526.    }
  527. #ifdef DEBUG
  528.    prtab("pc1tab", perm, 8);
  529. #endif
  530.    init_perm(PC1ROT, perm, 8);
  531.  
  532.    /*
  533.     * PC2ROT - PC2 inverse, then Rotate (once or twice), then PC2.
  534.     */
  535.    for (j = 0; j < 2; j++) {
  536.       unsigned char pc2inv[64];
  537.       for (i = 0; i < 64; i++)
  538.          perm[i] = pc2inv[i] = 0;
  539.       for (i = 0; i < 64; i++) {
  540.          if ((k = PC2[i]) == 0)
  541.             continue;
  542.          pc2inv[k-1] = i+1;
  543.       }
  544.       for (i = 0; i < 64; i++) {
  545.          if ((k = PC2[i]) == 0)
  546.             continue;
  547.          k += j;
  548.          if ((k%28) <= j) k -= 28;
  549.          perm[i] = pc2inv[k];
  550.       }
  551. #ifdef DEBUG
  552.       prtab("pc2tab", perm, 8);
  553. #endif
  554.       init_perm(PC2ROT[j], perm, 8);
  555.    }
  556.  
  557.    /*
  558.     * Bit reverse, then initial permutation, then expansion.
  559.     */
  560.    for (i = 0; i < 8; i++) {
  561.       for (j = 0; j < 8; j++) {
  562.          k = (j < 2)? 0: IP[ExpandTr[i*6+j-2]-1];
  563.          if (k > 32)
  564.             k -= 32;
  565.          else if (k > 0)
  566.             k--;
  567.          if (k > 0) {
  568.             k--;
  569.             k = (k|07) - (k&07);
  570.             k++;
  571.          }
  572.          perm[i*8+j] = k;
  573.       }
  574.    }
  575. #ifdef DEBUG
  576.    prtab("ietab", perm, 8);
  577. #endif
  578.    init_perm(IE3264, perm, 8);
  579.  
  580.    /*
  581.     * Compression, then final permutation, then bit reverse.
  582.     */
  583.    for (i = 0; i < 64; i++) {
  584.       k = IP[CIFP[i]-1];
  585.       if (k > 0) {
  586.          k--;
  587.          k = (k|07) - (k&07);
  588.          k++;
  589.       }
  590.       perm[k-1] = i+1;
  591.    }
  592. #ifdef DEBUG
  593.    prtab("cftab", perm, 8);
  594. #endif
  595.    init_perm(CF6464, perm, 8);
  596.  
  597.    /*
  598.     * SPE table
  599.     */
  600.    for (i = 0; i < 48; i++)
  601.       perm[i] = P32Tr[ExpandTr[i]-1];
  602.    for (tableno = 0; tableno < 8; tableno++) {
  603.       for (j = 0; j < 64; j++)  {
  604.          k = (((j >> 0) &01) << 5)|
  605.              (((j >> 1) &01) << 3)|
  606.              (((j >> 2) &01) << 2)|
  607.              (((j >> 3) &01) << 1)|
  608.              (((j >> 4) &01) << 0)|
  609.              (((j >> 5) &01) << 4);
  610.          k = S[tableno][k];
  611.          k = (((k >> 3)&01) << 0)|
  612.              (((k >> 2)&01) << 1)|
  613.              (((k >> 1)&01) << 2)|
  614.              (((k >> 0)&01) << 3);
  615.          for (i = 0; i < 32; i++)
  616.             tmp32[i] = 0;
  617.          for (i = 0; i < 4; i++)
  618.             tmp32[4 * tableno + i] = (k >> i) & 01;
  619.          k = 0;
  620.          for (i = 24; --i >= 0; )
  621.             k = (k<<1) | tmp32[perm[i]-1];
  622.          TO_SIX_BIT(SPE[0][tableno][j], k);
  623.          k = 0;
  624.          for (i = 24; --i >= 0; )
  625.             k = (k<<1) | tmp32[perm[i+24]-1];
  626.          TO_SIX_BIT(SPE[1][tableno][j], k);
  627.       }
  628.    }
  629. }
  630.  
  631. /*
  632.  * Initialize "perm" to represent transformation "p", which rearranges
  633.  * (perhaps with expansion and/or contraction) one packed array of bits
  634.  * (of size "chars_in" characters) into another array (of size "chars_out"
  635.  * characters).
  636.  *
  637.  * "perm" must be all-zeroes on entry to this routine.
  638.  */
  639. STATIC init_perm(perm, p, chars_out)
  640.    C_block perm[64/CHUNKBITS][1<<CHUNKBITS];
  641.    unsigned char p[64];
  642.    int chars_out; {
  643.    register int i, j, k, l;
  644.  
  645.    for (k = 0; k < chars_out*8; k++) {   /* each output bit position */
  646.       l = p[k] - 1;      /* where this bit comes from */
  647.       if (l < 0)
  648.          continue;   /* output bit is always 0 */
  649.       i = l>>LGCHUNKBITS;   /* which chunk this bit comes from */
  650.       l = 1<<(l&(CHUNKBITS-1));   /* mask for this bit */
  651.       for (j = 0; j < (1<<CHUNKBITS); j++) {   /* each chunk value */
  652.          if ((j & l) != 0)
  653.             perm[i][j].b[k>>3] |= 1<<(k&07);
  654.       }
  655.    }
  656. }
  657.  
  658. /*
  659.  * "setkey" routine (for backwards compatibility)
  660.  */
  661. setkey(key)
  662.    register const char *key; {
  663.    register int i, j, k;
  664.    C_block keyblock;
  665.  
  666.    for (i = 0; i < 8; i++) {
  667.       k = 0;
  668.       for (j = 0; j < 8; j++) {
  669.          k <<= 1;
  670.          k |= (unsigned char)*key++;
  671.       }
  672.       keyblock.b[i] = k;
  673.    }
  674.    return (des_setkey((char *)keyblock.b));
  675. }
  676.  
  677. /*
  678.  * "encrypt" routine (for backwards compatibility)
  679.  */
  680. encrypt(block, flag)
  681.    register char *block;
  682.    int flag; {
  683.    register int i, j, k;
  684.    C_block cblock;
  685.  
  686.    for (i = 0; i < 8; i++) {
  687.       k = 0;
  688.       for (j = 0; j < 8; j++) {
  689.          k <<= 1;
  690.          k |= (unsigned char)*block++;
  691.       }
  692.       cblock.b[i] = k;
  693.    }
  694.    if (des_cipher((char *)&cblock, (char *)&cblock, 0L, (flag ? -1: 1)))
  695.       return (1);
  696.    for (i = 7; i >= 0; i--) {
  697.       k = cblock.b[i];
  698.       for (j = 7; j >= 0; j--) {
  699.          *--block = k&01;
  700.          k >>= 1;
  701.       }
  702.    }
  703.    return (0);
  704. }
  705.  
  706. #ifdef DEBUG
  707. STATIC prtab(s, t, num_rows)
  708.    char *s;
  709.    unsigned char *t;
  710.    int num_rows; {
  711.    register int i, j;
  712.  
  713.    (void)printf("%s:\n", s);
  714.    for (i = 0; i < num_rows; i++) {
  715.       for (j = 0; j < 8; j++) {
  716.           (void)printf("%3d", t[i*8+j]);
  717.       }
  718.       (void)printf("\n");
  719.    }
  720.    (void)printf("\n");
  721. }
  722. #endif
  723.